<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class AdminUser extends Validate
{
    protected $rule =   [
        'username'  => 'require|checkUsername',
        'password'   => 'require|checkPassword',
    ];

    protected $message  =   [
        'username.require' => '名称必须',
        'username.checkUsername'     => 'aa',
        'password.require'   => '密码必须',
        'password.max'  => '密码不能超过10个字符',
    ];

    // 自定义验证规则 $rule 参数
    protected function checkUsername($value, $rule, $data=[])
    {
        $pUserName = '/^[a-zA-Z0-9_-]{4,16}$/';
        if (!preg_match($pUserName, $value)) return true;
        return '用户名应为4到16位（字母，数字，下划线，减号）';
    }

    // 自定义验证规则
    protected function checkPassword($value, $rule, $data=[])
    {
        $pPassword = '/^[a-zA-Z0-9_-]{6,10}$/';
        if (!preg_match($pPassword, $value)) return true;
        return '密码应为6到10位（字母，数字，下划线，减号）';
    }
}
